package com.pcbly.web.components.utils.template.pagemess;

import java.io.File;
import java.lang.reflect.Field;

import org.apache.commons.io.FileUtils;

public class ModelPageList extends ModelPage {

	private String listHead = "";

	private String listCol = "";

	private String listDateCol = "";

	private String listDoubleCol = "";

	private String listIntCol = "";

	public String getListHead() {
		return listHead;
	}

	public void setListHead(String listHead) {
		this.listHead = listHead;
	}

	public ModelPageList() {
		StringBuilder sb = new StringBuilder();
		sb.append("		<td align=\"left\" <html:if test=\"asc\"> ");
		sb.append("onclick=\"javascript:imgOnclick('#{fieldName}',false);\"");
		sb.append("</html:if><html:else> ");
		sb.append("onclick=\"javascript:imgOnclick('#{fieldName}',true);\" ");
		sb.append("</html:else>>");
		sb.append("<html:text name=\"#{firstEntityL}.#{fieldName}\" />");
		sb.append("<html:if test=\"orderBy=='#{fieldName}'\">&nbsp;<img ");
		sb.append(" class=\"sort-arrow\" ");
		sb
				.append("<html:if test=\"asc\"> src=\"<%=WEBPATH%>/images/arrow_up.png\"</html:if>");
		sb
				.append("<html:else> src=\"<%=WEBPATH%>/images/arrow_down.png\"</html:else> ");
		sb.append("/> </html:if></td>\r\n");
		setListHead(sb.toString());

		sb = new StringBuilder();
		sb
				.append("					<td align=\"left\"><html:property value=\"#{fieldName}\" /></td>\r\n");
		setListCol(sb.toString());

		sb = new StringBuilder();
		sb.append("					<td align=\"left\">");
		sb.append("<html:text name=\"global.format.date\">");
		sb.append("<html:param value=\"#{fieldName}\" /></html:text></td>\r\n");
		setListDateCol(sb.toString());

		sb = new StringBuilder();
		sb.append("					<td align=\"left\">");
		sb.append("<html:text name=\"global.format.money\">");
		sb.append("<html:param value=\"#{fieldName}\" /></html:text></td>\r\n");
		setListDoubleCol(sb.toString());

		sb = new StringBuilder();
		sb.append("					<td align=\"left\">");
		sb.append("<html:text name=\"global.format.int\">");
		sb.append("<html:param value=\"#{fieldName}\" /></html:text></td>\r\n");
		setListIntCol(sb.toString());
	}

	public String getListCol() {
		return listCol;
	}

	public void setListCol(String listCol) {
		this.listCol = listCol;
	}

	public String getListDateCol() {
		return listDateCol;
	}

	public void setListDateCol(String listDateCol) {
		this.listDateCol = listDateCol;
	}

	public String getListDoubleCol() {
		return listDoubleCol;
	}

	public void setListDoubleCol(String listDoubleCol) {
		this.listDoubleCol = listDoubleCol;
	}

	public String getListIntCol() {
		return listIntCol;
	}

	public void setListIntCol(String listIntCol) {
		this.listIntCol = listIntCol;
	}

	public void product() {
		try {
			File file = new File(getPagePath("list.jsp"));
			// System.out.println(file.getAbsolutePath());
			/************** page_list.txt **************************/
			String name = "page_list.txt";
			StringBuilder sb = new StringBuilder();
			File fileXML = new File(model.getFilePath(packName, name));
			String mainTxt = model.readTpl(fileXML);

			String dataModel = productListDataModel();
			mainTxt = mainTxt.replaceAll("\\#\\{dataModel\\}", dataModel);
			String head = productListHead();
			mainTxt = mainTxt.replaceAll("\\#\\{head\\}", head);
			int icols = fields.length;
			boolean iflag = false;
			for (Field field : fields) {
				if (field == null) {
					iflag = true;
					break;
				}
			}
			icols = iflag ? icols - 1 : icols + 1;
			mainTxt = mainTxt.replaceAll("\\#\\{cols\\}", "" + icols);
			sb.append(mainTxt);
			FileUtils.writeStringToFile(file, sb.toString(), "UTF-8");
			sb.delete(0, sb.length());
			if (file != null) {
				file = null;
			}
			if (fileXML != null) {
				fileXML = null;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private String productListHead() {
		StringBuilder sb = new StringBuilder();
		for (Field field : fields) {
			if (field == null) {
				break;
			}
			String head = listHead.replaceAll("\\#\\{fieldName\\}", field
					.getName());
			head = head.replaceAll("\\#\\{firstEntityL\\}", model
					.getFirstEntityL());
			sb.append(head);
		}
		return sb.toString();
	}

	private String productListDataModel() {
		StringBuilder sb = new StringBuilder();
		for (Field field : fields) {
			if (field == null) {
				break;
			}
			String typeName = field.getType().getName();
			typeName = typeName.toLowerCase();
			String col = "";
			if (typeName.indexOf("date") > 0) {
				col = listDateCol.replaceAll("\\#\\{fieldName\\}", field
						.getName());
			} else if (typeName.indexOf("int") > 0) {
				col = listIntCol.replaceAll("\\#\\{fieldName\\}", field
						.getName());
			} else if (typeName.indexOf("double") > 0) {
				col = listDoubleCol.replaceAll("\\#\\{fieldName\\}", field
						.getName());
			} else {
				col = listCol.replaceAll("\\#\\{fieldName\\}", field.getName());
			}
			sb.append(col);
		}
		return sb.toString();
	}
}
